#include "Tools.h"
#include "StringConverter.h"

//-----------------------------------------------------------------------
String Tools::GetCurDateTime(time_t t)
{
	if (0 == t)
		t = time(0);
	tm ttm = *localtime(&t);
	char buf[20];
	strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S", &ttm);
	return buf;
}
//-----------------------------------------------------------------------
uint32 Tools::GetCurDate(uint32 now_time)
{
	if(!now_time)
		now_time = time(0);
	
	time_t ti = (time_t)now_time;
	tm curTm = *localtime(&ti);
	char buf[20];
	strftime(buf, sizeof buf, "%Y%m%d", &curTm);
	return StringConverter::parseUnsignedInt(buf);
}
//-----------------------------------------------------------------------
uint32 Tools::GetCurTimer(void)
{
	time_t ti = time(0);
	tm curTm = *localtime(&ti);
	char buf[20];
	strftime(buf, sizeof buf, "%H%M%S", &curTm);
	return StringConverter::parseUnsignedInt(buf);
}
//-----------------------------------------------------------------------
time_t Tools::GetDateTimeSeconds(String &str)
{
	if (str == ""|| str == "0000-00-00 00:00:00")
		return 0;
	tm tmsd;
	memset(&tmsd, 0, sizeof(tmsd));
	sscanf(str.c_str(), "%4d-%2d-%2d %2d:%2d:%2d", &tmsd.tm_year, &tmsd.tm_mon, &tmsd.tm_mday, &tmsd.tm_hour, &tmsd.tm_min, &tmsd.tm_sec);
	tmsd.tm_year -= 1900;
	tmsd.tm_mon -= 1;
	return mktime(&tmsd);
}
//-----------------------------------------------------------------------
bool Tools::IsSameDate(time_t t1, time_t t2)
{
	if (0 == t1)
		return false;
	if (0 == t2)
		t2 = time(0);
	tm t1Tm = *(localtime(&t1));
	tm *pt2Tm = localtime(&t2);
	return (pt2Tm->tm_yday == t1Tm.tm_yday && pt2Tm->tm_year == t1Tm.tm_year);
}
//-----------------------------------------------------------------------
bool Tools::IsSameYear(time_t t1, time_t t2)
{
	if (0 == t1)
		return false;
	if (0 == t2)
		t2 = time(0);
	tm t1Tm = *(localtime(&t1));
	tm *pt2Tm = localtime(&t2);
	return (pt2Tm->tm_year == t1Tm.tm_year);
}

//-----------------------------------------------------------------------
bool Tools::IsSameMonth(time_t t1, time_t t2)
{
	if (0 == t1)
		return false;
	if (0 == t2)
		t2 = time(0);
	tm t1Tm = *(localtime(&t1));
	tm *pt2Tm = localtime(&t2);
	return (pt2Tm->tm_mon == t1Tm.tm_mon);
}

//-----------------------------------------------------------------------
bool Tools::IsSameDay(time_t t1, time_t t2)
{
	if (0 == t1)
		return false;
	if (0 == t2)
		t2 = time(0);
	tm t1Tm = *(localtime(&t1));
	tm *pt2Tm = localtime(&t2);
	return (pt2Tm->tm_mday == t1Tm.tm_mday);
}

bool Tools::IsSameWeek(time_t newtime, time_t oldtime /* = 0 */)
{
	if(newtime == 0 || oldtime == 0)
		return false;
	
	if(newtime < oldtime)
		return false;
	
	time_t tt = GetMondayTime(newtime);
	return oldtime >= tt;
}

uint32 Tools::GetMondayTime(time_t nowtime)
{
	time_t ntime = time(0);
	if (nowtime)
		ntime = nowtime;	
	tm newtm = *localtime(&ntime);	
	if (newtm.tm_wday == 0)
	{
		newtm.tm_mday -= 6;
	}
	else
	{
		newtm.tm_mday = newtm.tm_mday - newtm.tm_wday +1;
	}
	
	newtm.tm_hour = 0;
	newtm.tm_min = 0;
	newtm.tm_sec = 0;
	return mktime(&newtm);
}

uint32 Tools::GetDifferDay(time_t oldtime, time_t newtime /* = 0 */)
{
	if(oldtime == 0)
		return 0;
	
	if(newtime == 0)
		newtime = time(0);
	
	tm newtm = *localtime(&newtime);	
	newtm.tm_hour = 0;// 把时间调到0点
	newtm.tm_min = 0;
	newtm.tm_sec = 0;
	time_t temp = mktime(&newtm);
	
	if(oldtime >= temp)
		return 0;
	
	uint32 diff = temp - oldtime;
	return uint32(ceil((float)diff / float(24 * 60*60)));	
}

uint32 Tools::GetOffsetDate(time_t oldtime, time_t newtime /* = 0 */)
{
	if(oldtime == 0)
		return 0;
	
	if(newtime == 0)
		newtime = time(0);
	
	tm oldtm = *localtime(&oldtime);	
	oldtm.tm_hour = 0;// 把时间调到0点
	oldtm.tm_min = 0;
	oldtm.tm_sec = 0;
	time_t temp1 = mktime(&oldtm);
	
	tm newtm = *localtime(&newtime);	
	newtm.tm_hour = 0;// 把时间调到0点
	newtm.tm_min = 0;
	newtm.tm_sec = 0;
	time_t temp2 = mktime(&newtm);
	
	if(temp1 >= temp2)
		return 0;
	
	uint32 diff = temp2 - temp1;
	return uint32(ceil((float)diff / float(24 * 60*60)));	
}

uint32 Tools::FormatDataCeil(time_t ntime)
{
	tm oldtm = *localtime(&ntime);	
	oldtm.tm_hour = 23;// 把时间调到0点
	oldtm.tm_min = 59;
	oldtm.tm_sec = 59;
	return mktime(&oldtm);
}

uint32 Tools::FormatDataCeilMonth(time_t ntime)
{
	tm oldtm = *localtime(&ntime);
	uint8 days = 31;
	
	switch(oldtm.tm_mon)
	{
	case 1:
		{
			days = 28;
			if(oldtm.tm_year % 4 == 0)	//到2200年 完全够判断了
				days = 29;
		}
		break;
	case 2:
		{
			days = 31;
		}
		break;
	case 3:
		{
			days = 30;
		}
		break;
	case 4:
		{
			days = 31;
		}
		break;
	case 5:
		{
			days = 30;
		}
		break;
	case 6:
		{
			days = 31;
		}
		break;
	case 7:
		{
			days = 31;
		}
		break;
	case 8:
		{
			days = 30;
		}
		break;
	case 9:
		{
			days = 31;
		}
		break;
	case 10:
		{
			days = 30;
		}
		break;
	case 11:
		{
			days = 31;
		}
		break;
	}
	
	oldtm.tm_mday = days;
	
	oldtm.tm_hour = 23;// 把时间调到0点
	oldtm.tm_min = 59;
	oldtm.tm_sec = 59;
	return mktime(&oldtm);
}
